// Kony


#include "AbilitySystem/Abilitys/BaseSummonAbility.h"



//获取随从召唤位置
TArray<FVector> UBaseSummonAbility::GetSpawnLocations()
{
	//获取玩家视角面向 方向 向量
	const FVector Forward = GetAvatarActorFromActorInfo()->GetActorForwardVector();
	//获取玩家位置
	const FVector Location = GetAvatarActorFromActorInfo()->GetActorLocation();
	//根据生成随从数量 计算角度
	const float DeltaSpread = SpawnSpread / NumMinions;
	// 计算向右延申向量：将前进向量绕上向量旋转(Spread角度/2)度
	// const FVector RightOfSpread = Forward.RotateAngleAxis(SpawnSpread /2.f, FVector::UpVector);
	// 计算向左延申向量：将前进向量绕上向量旋转(Spread角度/2)度
	const FVector LeftOfSpread= Forward.RotateAngleAxis(-SpawnSpread /2.f, FVector::UpVector);
	//随从生成位置向量数组
	TArray<FVector> SpawnLocations;
	//根据召唤数量，从左到右 绘制对应数量的线条，并在这些线条的最小到最大距离 随机获取位置生成随从
	for (int32 i = 0; i < NumMinions; i++)
	{
		//从左到右获取当前随从对应的线条位置
		const FVector Direction = LeftOfSpread.RotateAngleAxis(i * DeltaSpread, FVector::UpVector);
		//获取随机生成随从位置
		FVector ChosenSpawnLocation = Location + Direction * FMath::RandRange(MinSpawnDistance, MaxSpawnDistance);
		//发射射线检测是否处于地面上，确保召唤物在地面生成
		FHitResult Hit;
		GetWorld()->LineTraceSingleByChannel(Hit, ChosenSpawnLocation+FVector(0.f,0.f,400.f), ChosenSpawnLocation - FVector(0.f,0.f,400.f), ECollisionChannel::ECC_Visibility);
		//如果ChosenSpawnLocation 位于空中，则使用射线击中地面的位置作为生成位置
		if (Hit.bBlockingHit)
		{
			ChosenSpawnLocation = Hit.ImpactPoint;
		}
		SpawnLocations.Add(ChosenSpawnLocation);
		//模拟创建随从绘制球体
		// DrawDebugSphere(GetWorld(),ChosenSpawnLocation, 15.f,12, FColor::Red, false, 3.f);
		// UKismetSystemLibrary::DrawDebugArrow(GetAvatarActorFromActorInfo(), Location, Location + Direction * MaxSpawnDistance, 4.f, FLinearColor::Green, 3.f);
	}
	// //最小距离绘制球体
	// DrawDebugSphere(GetWorld(),Location + LeftOfSpread * MinSpawnDistance, 15.f,12, FColor::Red, false, 3.f);
	// //最大距离绘制球体
	// DrawDebugSphere(GetWorld(),Location + LeftOfSpread * MaxSpawnDistance, 15.f,12, FColor::Red, false, 3.f);

	return SpawnLocations;
}
//从MinionClasses设置的随从数组中随机获取
TSubclassOf<APawn> UBaseSummonAbility::GetRandomMinionClass()
{
	const int32 Selection = FMath::RandRange(0, MinionClasses.Num() - 1);
	return MinionClasses[Selection];
}
